
$(function() {	
	$("input[id='emp']:visible").autocomplete(Test.emp);
	
	_ck_default("input[id='emp']:visible", "input[id^='emp_t']:visible");
		
	_change("input[id='address']:visible", "select[id^='address_t']:visible");
	
	_change("input[id='type']:visible", "select[id^='type_t']:visible");
	
	$('#date').datepicker(_now_option);

	Tip.supplier();
	
	$('#supplier').blur(function() {		
		var a = $(this).val();
		
		var body = { };
		body.id = a;
		$.getJSON('contact.php?do=Supplier.ajax_id', body, function(json) {
			$("#c_name").val(json.name);
			$("#c_fullname").val(json.fullname);
			$("#c_address").val(json.address);
			$("#c_web").val(json.url);
			
			$("#currency").val(json.currency);
			$("#type").val(json.pay);
			$("#tax").val(json.tax);
		});
		
		var body = {};
		body.q = a;
		
		$.getJSON('finance.php?do=PaymentMy.ajax_linkman', body, function(json) {			
			Supplier.linkman(json);
		});
	});
	
	$("#label_linkman").hide();

	$(":text").change(function() {
		cart_update_price(this.name, $(this).val());
    });	
});

function cookie_cart2() {
	var json = $.cookie('cart1');
	if (json != null && json.length > 0) {
		return $.evalJSON(json);
	}

	return new Array();
}

function cart_update_price(name, value) {
	if (!/^price_/.test(name)) {
		return false;
	}

	if (eval(value) < 0) {
		return false;
	}

	var cart = cookie_cart2();

	var cc = name.split("_");
	var id = cc[1];

	if (!cart_exist(id)) {
		var mm = [eval(id), eval(value)];
		cart.push(mm);
	}

	var aa = new Array();
	for (var i = 0; i < cart.length; i++) {
		if (cart[i][0] == id) {
			cart[i][1] = eval(value);
		}

		aa.push(cart[i]);
	}

	$.cookie('cart1', $.toJSON(aa));
}

function cart_exist(id) {
	var cart = cookie_cart2();
	for (var i = 0; i < cart.length; i++) {
		if (cart[i][0] == id) {
			return true;
		}
	}

	return false;
}
